<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发票来源" prop="invoiceSource">
        <el-input
          v-model="queryParams.invoiceSource"
          placeholder="请输入发票来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="转内销证明编号" prop="proofNumber">
        <el-input
          v-model="queryParams.proofNumber"
          placeholder="请输入转内销证明编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数电发票号码" prop="eInvoiceNumber">
        <el-input
          v-model="queryParams.eInvoiceNumber"
          placeholder="请输入数电发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票代码" prop="invoiceCode">
        <el-input
          v-model="queryParams.invoiceCode"
          placeholder="请输入发票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票号码" prop="invoiceNumber">
        <el-input
          v-model="queryParams.invoiceNumber"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开票日期" prop="invoiceDate">
        <el-date-picker clearable
          v-model="queryParams.invoiceDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开票日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销售方纳税人识别号" prop="sellerTaxId">
        <el-input
          v-model="queryParams.sellerTaxId"
          placeholder="请输入销售方纳税人识别号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售方纳税人名称" prop="sellerName">
        <el-input
          v-model="queryParams.sellerName"
          placeholder="请输入销售方纳税人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税额" prop="taxAmount">
        <el-input
          v-model="queryParams.taxAmount"
          placeholder="请输入税额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效抵扣税额" prop="deductibleTax">
        <el-input
          v-model="queryParams.deductibleTax"
          placeholder="请输入有效抵扣税额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="票种" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" placeholder="请选择票种" clearable>
          <el-option
            v-for="dict in dict.type.invoice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="票种标签" prop="invoiceTag">
        <el-input
          v-model="queryParams.invoiceTag"
          placeholder="请输入票种标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择发票状态" clearable>
          <el-option
            v-for="dict in dict.type.invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发票风险等级" prop="riskLevel">
        <el-input
          v-model="queryParams.riskLevel"
          placeholder="请输入发票风险等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择记录最后更新时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="部门状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择部门状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['md:input:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['md:input:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:input:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:input:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inputList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键，唯一标识每张进项发票" align="center" prop="serialNum" />
      <el-table-column label="进项发票唯一值id" align="center" prop="inId" />
      <el-table-column label="发票来源" align="center" prop="invoiceSource" />
      <el-table-column label="转内销证明编号" align="center" prop="proofNumber" />
      <el-table-column label="数电发票号码" align="center" prop="eInvoiceNumber" />
      <el-table-column label="发票代码" align="center" prop="invoiceCode" />
      <el-table-column label="发票号码" align="center" prop="invoiceNumber" />
      <el-table-column label="开票日期" align="center" prop="invoiceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售方纳税人识别号" align="center" prop="sellerTaxId" />
      <el-table-column label="销售方纳税人名称" align="center" prop="sellerName" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="税额" align="center" prop="taxAmount" />
      <el-table-column label="有效抵扣税额" align="center" prop="deductibleTax" />
      <el-table-column label="票种" align="center" prop="invoiceType" />
      <el-table-column label="票种标签" align="center" prop="invoiceTag" />
      <el-table-column label="发票状态" align="center" prop="invoiceStatus" />
      <el-table-column label="发票风险等级" align="center" prop="riskLevel" />
      <el-table-column label="记录创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录最后更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="部门状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['md:input:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:input:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改进项发票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">

          <!-- 基础信息 -->
          <el-col :span="12">
            <el-form-item label="发票来源" prop="invoiceSource">
              <el-input v-model="form.invoiceSource" placeholder="请输入发票来源" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="转内销证明编号" prop="proofNumber">
              <el-input v-model="form.proofNumber" placeholder="请输入转内销证明编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数电发票号码" prop="eInvoiceNumber">
              <el-input v-model="form.eInvoiceNumber" placeholder="请输入数电发票号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票代码" prop="invoiceCode">
              <el-input v-model="form.invoiceCode" placeholder="请输入发票代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票号码" prop="invoiceNumber">
              <el-input v-model="form.invoiceNumber" placeholder="请输入发票号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票日期" prop="invoiceDate">
              <el-date-picker clearable
                              v-model="form.invoiceDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开票日期"
                              style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售方纳税人识别号" prop="sellerTaxId">
              <el-input v-model="form.sellerTaxId" placeholder="请输入销售方纳税人识别号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售方纳税人名称" prop="sellerName">
              <el-input v-model="form.sellerName" placeholder="请输入销售方纳税人名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税额" prop="taxAmount">
              <el-input v-model="form.taxAmount" placeholder="请输入税额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效抵扣税额" prop="deductibleTax">
              <el-input v-model="form.deductibleTax" placeholder="请输入有效抵扣税额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票种" prop="invoiceType">
              <el-select v-model="form.invoiceType" placeholder="请选择票种" clearable style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.invoice_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="票种标签" prop="invoiceTag">
              <el-input v-model="form.invoiceTag" placeholder="请输入票种标签" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票状态" prop="invoiceStatus">
              <el-select v-model="form.invoiceStatus" placeholder="请选择发票状态" clearable style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.invoice_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票风险等级" prop="riskLevel">
              <el-input v-model="form.riskLevel" placeholder="请输入发票风险等级" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="记录创建时间" prop="createdAt">
              <el-date-picker clearable
                              v-model="form.createdAt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择记录创建时间"
                              style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录最后更新时间" prop="updatedAt">
              <el-date-picker clearable
                              v-model="form.updatedAt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择记录最后更新时间"
                              style="width: 100%;" />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="部门状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择部门状态" clearable style="width: 100%;">
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="删除标志" prop="delFlag">
              <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
            </el-form-item>
          </el-col> -->

        </el-row>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </div>
          </el-dialog>
        </div>
</template>

<script>
import { listInput, getInput, delInput, addInput, updateInput } from "@/api/inv/inputinvoice";

export default {
  name: "Input",
  dicts: ['invoice_type', 'invoice_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进项发票信息表格数据
      inputList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inId: null,        invoiceSource: null,        proofNumber: null,        eInvoiceNumber: null,        invoiceCode: null,        invoiceNumber: null,        invoiceDate: null,        sellerTaxId: null,        sellerName: null,        amount: null,        taxAmount: null,        deductibleTax: null,        invoiceType: null,        invoiceTag: null,        invoiceStatus: null,        riskLevel: null,        createdAt: null,        updatedAt: null,        status: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询进项发票信息列表 */
    getList() {
      this.loading = true;
      listInput(this.queryParams).then(response => {
        this.inputList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        inId: null,        invoiceSource: null,        proofNumber: null,        eInvoiceNumber: null,        invoiceCode: null,        invoiceNumber: null,        invoiceDate: null,        sellerTaxId: null,        sellerName: null,        amount: null,        taxAmount: null,        deductibleTax: null,        invoiceType: null,        invoiceTag: null,        invoiceStatus: "0",        riskLevel: null,        createdAt: null,        updatedAt: null,        status: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进项发票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getInput(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进项发票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateInput(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInput(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除进项发票信息编号为"' + serialNums + '"的数据项？').then(function() {
        return delInput(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/input/export', {
        ...this.queryParams
      }, `input_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
